package com.trimps.steps.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hotfire.template.common.DateUtils;
import com.trimps.steps.exception.SqlOperationsException;
import com.trimps.steps.mapper.StepMapper;
import com.trimps.steps.pojo.Step;
import com.trimps.steps.pojo.TotalSteps;
import com.trimps.steps.pojo.Walker;
import com.trimps.steps.pojo.WalkerGroup;
import com.trimps.steps.service.IStepService;
import com.trimps.steps.service.IWalkerGroupService;
import com.trimps.steps.service.IWalkerService;

@Service("stepService")
public class StepServiceImpl implements IStepService {
    @Autowired
    private StepMapper stepMapper;  
    @Autowired
    private IWalkerService walkerService;  
    @Autowired
    private IWalkerGroupService walkerGroupService;

    @Override
    public List<Step> getAllSteps() {
        return getSteps(1, Integer.MAX_VALUE);
    }

    @Override
    public List<Step> getSteps(int currentPage, int rowPerPage) {
        return getSteps(currentPage, rowPerPage, new HashMap<String, String>());
    }

    @Override
    public int getStepCount() {
        return getStepCount(new HashMap<String, String>());
    }

    @Override
    public Step getLatestStepByUserId(int id) {
        return stepMapper.getLatestStepByUserId(id);
    }

    @Override
    public int getStepCount(HashMap<String, String> map) {
        return stepMapper.getStepCount(map);
    }

    @Override
    public List<Step> getSteps(int currentPage, int rowPerPage, HashMap<String, String> map) {
        return stepMapper.getSteps(currentPage, rowPerPage, map);
    }
    
    @Override
    public List<TotalSteps> getTotalSteps(HashMap<String, String> map) {
        return stepMapper.getTotalSteps(map);
    }

    @Override
    public int save(Step step) {
        return stepMapper.save(step);
    }

    @Override
    public int saveStep(int userId, int groupId, String realyName, int stepCount, String stepDate) throws SqlOperationsException {
        Step step = new Step();
        Walker walker = walkerService.getWalkerByRealyName(realyName);
        if(walker == null)
            throw new SqlOperationsException("该成员:"+realyName+"不存在！");
        
        boolean checkPermission = false;
        // 检查健步成员添加的组是否属于自己的组
        List<WalkerGroup> wgs = walkerGroupService.getWalkerGroupByUserId(userId);
        for(WalkerGroup wg : wgs){
            if(walker.getGroupId() == wg.getId()){
                checkPermission = true;
            }
        }
        if(!checkPermission){
            throw new SqlOperationsException("成员"+realyName+"添加到该分组的权限不够！");
        }
        step.setStepCount(stepCount);
        //step.setStepDate(new Date());
        step.setStepDate(DateUtils.dateFormat(stepDate, "yyyy-MM-dd"));
        step.setCreateUserId(userId);
        step.setWalker(walker);
        
        return stepMapper.save(step);
    }

    @Override
    public int delById(String stepId) {
        return stepMapper.delById(stepId);
    }
    
}
